package com.irun.pm.ui;

import java.io.File;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.irun.pm.R;
import com.irun.pm.controller.DriverInfoControllers;
import com.irun.pm.controller.EnforcementControllers;
import com.irun.pm.mode.DriverInfo;
import com.irun.pm.utils.FileUtils;
import com.irun.pm.utils.ImageUtil;
import com.irun.pm.utils.StringUtil;
import com.irun.pm.utils.TakePhotoUtils;
import com.irun.pm.utils.Utils;

/***
 * @author huangsm
 * @date 2012-5-27
 * @email huangsanm@gmail.com
 * @version 1.0
 * @description 现场执法详情
 */
public class EnforcementDetailActivity extends BaseActivity implements OnClickListener {

	private String mPhoto;
	private DriverInfo e;
	private static final File PHOTO_DIR = new File(
			Environment.getExternalStorageDirectory() + "/DCIM/Camera");
	private static final int REQUES_CAMERA = 0;
	private File mCurrentPhotoFile;
	private DriverInfoControllers dc ;
	private EnforcementControllers controllers;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.enforcement_detail);
		String id = getIntent().getStringExtra("id");
		dc = new DriverInfoControllers(this);
		controllers = new EnforcementControllers(this);
		//e = controllers.queryDriverInfoById(id);
		e = controllers.getEnforcementById(id);
		if(e == null){
			Toast.makeText(this, "请求数据失败，请检查服务连接是否成功！", 3000).show();
			return;
		}
		((TextView)findViewById(R.id.tvEnfNativeAddress)).setText(StringUtil.getValue(e.getNativeAddress()));
		((TextView)findViewById(R.id.tvEnfDate)).setText(StringUtil.getValue(e.getDate()));
		((TextView)findViewById(R.id.tvEnfAddress)).setText(StringUtil.getValue(e.getAddress()));
		((TextView)findViewById(R.id.tvEnfCarNumber)).setText(StringUtil.getValue(e.getCardNumber()));
		((TextView)findViewById(R.id.tvEnfName)).setText(StringUtil.getValue(e.getName()));
		((TextView)findViewById(R.id.tvEnfSex)).setText(StringUtil.getValue(e.getSex()));
		((TextView)findViewById(R.id.tvEnfcard)).setText(StringUtil.getValue(e.getCard()));
		((TextView)findViewById(R.id.tvEnfDriveAge)).setText(StringUtil.getValue(e.getDriverAge()));
		((TextView)findViewById(R.id.tvEnfNote)).setText(StringUtil.getValue(e.getNote()));
		((TextView)findViewById(R.id.tvEnfresult)).setText(StringUtil.getValue(e.getResult()));
		((TextView)findViewById(R.id.tvEnfDealPeople)).setText(StringUtil.getValue(e.getDeal_people()));
		findViewById(R.id.ivEnfDetailPhoto).setOnClickListener(this);
		mPhoto = e.getPhotoPath();
		if(!StringUtil.isEmpty(e.getPhotoPath())){
			Bitmap bmp = Utils.getBitmapByPath(e.getPhotoPath(), 100, 100);
			if(bmp == null){
				bmp = BitmapFactory.decodeResource(getResources(), R.drawable.default_photo);
			}
			((ImageView)findViewById(R.id.ivEnfDetailPhoto)).setImageBitmap(bmp);
		}else{
			Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.default_photo);
			((ImageView)findViewById(R.id.ivEnfDetailPhoto)).setImageBitmap(bmp);
		}
		Button btn_print = (Button) findViewById(R.id.btnPrint);
		btn_print.setOnClickListener(this);
		findViewById(R.id.btnUpload).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnPrint:
			Toast.makeText(this, "抱歉，暂时无法接入您的打印设备!", Toast.LENGTH_SHORT).show();
			break;
		case R.id.ivEnfDetailPhoto:
			if(e!=null && !StringUtil.isEmpty(e.photoPath)){
				Intent intent = new Intent(this,PhotoView.class);
				intent.putExtra("path", e.photoPath);
				startActivity(intent);
			}else{
				Toast.makeText(getBaseContext(), "该记录未上传图片", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.btnUpload:
			TakePhotoUtils takePhotoUtils = new TakePhotoUtils(this);
			PHOTO_DIR.mkdirs();
			mCurrentPhotoFile = new File(PHOTO_DIR,
					TakePhotoUtils.getPhotoFileName());
			Intent intent = takePhotoUtils.doTakePhoto(mCurrentPhotoFile);
			startActivityForResult(intent, REQUES_CAMERA);
			break;
		default:
			break;
		}
	}
	
	String filePath = "";

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_CANCELED) {
			return;
		}
		switch (requestCode) {
		case REQUES_CAMERA:
			Bitmap bmp = ImageUtil.compressHeadBitMap(mCurrentPhotoFile
					.getAbsolutePath());
			filePath = FileUtils.FILE_POLECE_IMAGE
					+ TakePhotoUtils.getPhotoFileName();
			ImageUtil.saveBmp(
					bmp,
					FileUtils.FILE_POLECE_IMAGE
							+ TakePhotoUtils.getPhotoFileName());
			((ImageView)findViewById(R.id.ivEnfDetailPhoto)).setImageBitmap(bmp);
			dc.updatePhotoPath(e, filePath);
			break;

		default:
			break;
		}
	}
}
